package com.ruoyi.service.impl;

import com.ruoyi.domain.UserDeviceData;
import com.ruoyi.mapper.UserDeviceDataMapper;
import com.ruoyi.service.UserDeviceDataService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lifei
 */
@Service
public class UserDeviceDataServiceImpl implements UserDeviceDataService {

    private final UserDeviceDataMapper userDeviceDataMapper;

    public UserDeviceDataServiceImpl(UserDeviceDataMapper userDeviceDataMapper) {
        this.userDeviceDataMapper = userDeviceDataMapper;
    }

    /**
     * 获取用户所有最新健康数据
     *
     * @param userId 用户ID
     * @return 用户所有最新健康数据
     */
    @Override
    public List<UserDeviceData> getUserLatestData(long userId) {
        return userDeviceDataMapper.selectUserLatestData(userId);
    }

    @Override
    public List<UserDeviceData> getUserDataByDate(Map<String, Object> requestBody) {
        final String USER_ID = "userId";
        final String DATE = "date";
        final String START_DATE = "startDate";
        final String END_DATE = "endDate";

        // 获取 userId
        if (!requestBody.containsKey(USER_ID)) {
            throw new IllegalArgumentException("用户 ID 不能为空");
        }
        Long userId = Long.valueOf(requestBody.get(USER_ID).toString());
        System.out.println("UserId: " + userId);

        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            // 解析 date 数组
            if (requestBody.containsKey(DATE)) {
                Object dateObj = requestBody.get(DATE);
                if (dateObj instanceof List<?>) {
                    List<?> dateList = (List<?>) dateObj;
                    if (dateList.size() == 2) {
                        startDate = dateFormat.parse(dateList.get(0).toString());
                        endDate = dateFormat.parse(dateList.get(1).toString());
                    } else {
                        throw new IllegalArgumentException("date 数组必须包含两个日期值");
                    }
                } else {
                    throw new IllegalArgumentException("date 格式错误，应为包含两个日期的数组");
                }
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("日期格式错误，应为 yyyy-MM-dd", e);
        }

        // 如果前端未传递日期范围，则默认查询最近 7 天
        if (startDate == null || endDate == null) {
            Calendar calendar = Calendar.getInstance();
            endDate = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, -7);
            startDate = calendar.getTime();
        }

        // 调用 Mapper 查询数据
        return userDeviceDataMapper.selectUserDataByDate(userId, startDate, endDate);
    }


    /**
     * app获取性别、身高、年龄
     * @return 用户数据列表
     */
    @Override
    public List<UserDeviceData> getUserInfoByUserId(long userId) {
        return userDeviceDataMapper.selectUserInfoByUserId(userId);
    }

    /**
     * 获取最近七次用户健康数据
     *
     * @return 睡眠时间数据列表
     */
    @Override
    public List<UserDeviceData> getWeekUserLastestSleepTimeData(long userId) {
        return userDeviceDataMapper.getWeekUserLastestSleepTimeData(userId);
    }

    /**
     * 获取用户最近五次血氧数据
     *
     * @param userId 用户ID
     * @return 血氧数据列表
     */
    @Override
    public List<UserDeviceData> selectUserFiveBloodOxygenData(long userId) {
        return userDeviceDataMapper.selectUserFiveBloodOxygenData(userId);
    }

    // 根据用户ID和目标时间获取步数
    @Override
    public Integer getTotalSteps(Long userId, Date goalDate) {
        return userDeviceDataMapper.getTotalStepsByUserIdAndDate(userId, goalDate);
    }

    // 根据用户ID和目标时间获取运动时间
    @Override
    public Integer getTotalActiveMinutes(Long userId, Date goalDate) {
        return userDeviceDataMapper.getTotalActiveMinutesByUserIdAndDate(userId, goalDate);
    }

    @Override
    public Integer selectActiveMinutes(Long userId) {
        return userDeviceDataMapper.getActiveMinutes(userId);
    }

}
